﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.ComponentModel;
using System.Windows.Forms;

namespace Engine.NetClasses
{
    public class ServerState:INotifyPropertyChanged
    {
        /// <summary>
        /// Счетчик сесий
        /// </summary>
        private int m_ssesionCounter=0;
        /// <summary>
        /// Общее число подключеных клиентов
        /// </summary>
        private int m_numConnectedSockets=0;      // the total number of clients connected to the server
        private int m_maxConnect;
        private bool State;
        public BaseServer Server;

        public bool Online
        {
            get { return State; }
            set { State = value;
            if (PropertyChanged != null)
            {
                PropertyChangedEventArgs e = new PropertyChangedEventArgs("Online");
                PropertyChanged(this, e);
            }
            }
        }
        

        public int MaxConnect
        {
            get { return m_maxConnect; }
            set { m_maxConnect = value; }
        }
        
        public uint SesionCounter 
        {
            get { return (uint)m_ssesionCounter; }            
        }
        public int NumConnectedSockets
        {
            get { return m_numConnectedSockets; }
        }

        public ServerState(int maxConnect)
        {
            m_numConnectedSockets = 0;
            m_ssesionCounter = 0;
            m_maxConnect = maxConnect;
            
        }
        public void ShowForm()
        {
            ServerStateForm form = new ServerStateForm();
            form.State = this;
            Thread t = new Thread(runForm);
            t.Start(form);

        }
        void runForm(object obj)
        {
            Application.Run((Form)obj);
        }

        public uint NewClient()
        {
            Interlocked.Increment(ref m_numConnectedSockets);
            Interlocked.Increment(ref m_ssesionCounter);
            if (PropertyChanged!=null)
            {
                PropertyChangedEventArgs e = new PropertyChangedEventArgs("SesionCounter");
                PropertyChanged(this, e);
            }
            if (PropertyChanged != null)
            {
                PropertyChangedEventArgs e = new PropertyChangedEventArgs("NumConnectedSockets");
                PropertyChanged(this, e);
            }
            return (uint)m_ssesionCounter;
        }
        public void ClientDisconect()
        {
            Interlocked.Decrement(ref m_numConnectedSockets);
            if (m_numConnectedSockets<0)
            {
                 Interlocked.Increment(ref m_numConnectedSockets);
            }
            if (PropertyChanged != null)
            {
                PropertyChangedEventArgs e = new PropertyChangedEventArgs("NumConnectedSockets");
                PropertyChanged(this, e);
            }
        }

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
    }
}
